<template>
    <div class="weather-container">
        <!-- 顶部导航 -->
        <header class="header">
            <h1>天气查询</h1>
            <form>
                <a-input v-model:value="selectedProvince" placeholder="输入省份" @change="fetchWeatherData"
                    class="province-select">
                </a-input>
            </form>
        </header>


        <!-- 天气卡片区域 -->
        <main class="content">
            <div v-if="loading" class="loading">
                <a-spin size="large" />
                <p>加载中...</p>
            </div>

            <div v-else-if="weatherData.length > 0" class="card-grid">
                <div v-for="(location, locIndex) in weatherData" :key="locIndex" class="location-card">
                    <div class="location-header">
                        <h2>{{ location.name }}</h2>
                        <p class="location-meta">{{ location.adm2 }} · {{ location.adm1 }}</p>

                    </div>

                    <div class="weather-forecast">
                        <div v-for="(day, dayIndex) in location.weathers" :key="dayIndex" class="daily-weather"
                            :class="{ 'today': dayIndex === 0 }">
                            <div class="date">{{ formatDate(day.fxDate) }}</div>
                            <div class="weather-icon">
                                <i>{{day.iconDay}}</i>
                            </div>
                            <div class="weather-text">{{ day.textDay }}</div>
                            <div class="temperature">
                                <span class="temp-min">{{ day.tempMin }}°</span>
                                <span class="temp-max">{{ day.tempMax }}°</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div v-else class="empty-state">
                <a-empty description="暂无天气数据" />
            </div>
        </main>

        <!-- 页脚 -->
        <footer class="footer">
            <p>天气数据来源: 和风天气</p>
        </footer>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import request from '@/utils/request';
import { message, Spin, Empty } from 'ant-design-vue';
import 'qweather-icons/font/qweather-icons.css';

// 状态管理
const selectedProvince = ref('上海');
const weatherData = ref([]);
const loading = ref(true);

// 格式化日期为星期几
const formatDate = (dateString) => {
    const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const date = new Date(dateString);
    return days[date.getDay()];
};

// 获取天气数据
const fetchWeatherData = async () => {
    loading.value = true;
    try {
        // 替换为实际后端接口
        let str = selectedProvince.value
        const response = await request.get(`weather/${str}`);
        weatherData.value = response.data;
        message.success(`已加载${selectedProvince.value}天气数据`);
    } catch (error) {
        console.error('获取天气数据失败:', error);
        message.error('获取天气数据失败，请稍后重试');
    } finally {
        loading.value = false;
    }
};

// 页面初始化时加载数据
onMounted(() => {
    fetchWeatherData();
});
</script>

<style scoped>
.weather-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 0;
    border-bottom: 1px solid #f0f0f0;
}

.header h1 {
    margin: 0;
    font-size: 24px;
    color: #333;
}

.province-select {
    width: 180px;
}

.content {
    padding: 30px 0;
}

.loading {
    text-align: center;
    padding: 50px 0;
}

.loading p {
    margin-top: 16px;
    color: #666;
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
}

.location-card {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    overflow: hidden;
    transition: transform 0.2s;
}

.location-card:hover {
    transform: translateY(-3px);
}

.location-header {
    padding: 16px 20px;
    border-bottom: 1px solid #f5f5f5;
}

.location-header h2 {
    margin: 0 0 8px 0;
    font-size: 18px;
    color: #1890ff;
}

.location-meta {
    margin: 0 0 12px 0;
    color: #666;
    font-size: 14px;
}

.details-link {
    color: #1890ff;
    font-size: 14px;
    text-decoration: none;
}

.details-link:hover {
    text-decoration: underline;
}

.weather-forecast {
    display: flex;
    padding: 16px 0;
    overflow-x: auto;
    scrollbar-width: thin;
}

.weather-forecast::-webkit-scrollbar {
    height: 4px;
}

.weather-forecast::-webkit-scrollbar-thumb {
    background-color: #ddd;
    border-radius: 2px;
}

.daily-weather {
    flex: 0 0 auto;
    width: 60px;
    text-align: center;
    padding: 0 8px;
}

.daily-weather.today {
    border-left: 2px solid #1890ff;
}

.date {
    font-size: 14px;
    color: #333;
    margin-bottom: 8px;
}

.weather-icon img {
    width: 36px;
    height: 36px;
    margin: 0 auto 8px;
}

.weather-text {
    font-size: 13px;
    color: #666;
    margin-bottom: 8px;
    white-space: nowrap;
}

.temperature {
    font-size: 13px;
}

.temp-min {
    color: #666;
    margin-right: 4px;
}

.temp-max {
    color: #f5222d;
}

.empty-state {
    padding: 50px 0;
    text-align: center;
}

.footer {
    text-align: center;
    padding: 20px 0;
    color: #999;
    font-size: 13px;
    border-top: 1px solid #f0f0f0;
    margin-top: 20px;
}
</style>
